<?php

namespace app\api\controller;

use app\common\business\WorkCollect;
use app\common\business\WorkRob;
use app\common\lib\Param;
use app\common\lib\Show;
use app\common\lib\Str;
use app\Request;
use think\facade\Cache;

class MyWork extends AuthBase
{
    //发布/更新零活信息
    public function save(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $id = input("id", 0, "intval");
        $type = input("type", 0, "intval");//work_type
        $name = $this->request->param("name", "", "trim");
        $start_time = $this->request->param("start_time", "", "trim");
        $population = input("population", 0, "intval");
        $valuation = input("valuation", 0, "intval");
        $price = $this->request->param("price", "0.00", "trim");
        $duration = $this->request->param("duration", "0.00", "trim");
        $address = $this->request->param("address", "", "trim");
        $longitude = $this->request->param("longitude", "", "trim");
        $latitude = $this->request->param("latitude", "", "trim");
        $describe = $this->request->param("describe", "", "trim");
        $attachment = $this->request->param("attachment", "", "trim");
        $is_enterprise = input("is_enterprise", 0, "intval");
        $department_id = input("department_id", 0, "intval");

        //参数校验
        $data = [
            'id' => $id,
            'type' => $type,
            'name' => $name,
            'start_time' => $start_time,
            'population' => $population,
            'valuation' => $valuation,
            'price' => $price,
            'duration' => $duration,
            'address' => $address,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'describe' => $describe,
            'attachment' => $attachment,
            'is_enterprise' => $is_enterprise,
            'department_id' => $department_id,
        ];
        $validate = new \app\api\validate\Work();
        if (!$validate->scene('save')->check($data)) {
            return Show::error([], $validate->getError());
        }

        $bisObj = new \app\common\business\Work();
        try {
            $info = $bisObj->saveData($this->userId,$data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$info) {
            return Show::error([], "保存失败");
        }

        return Show::success([], "保存成功");
    }

    //取消招工中的零活
    public function cancel(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $id = input("work_id", 0, "intval");

        $bisObj = new \app\common\business\Work();
        try {
            $info = $bisObj->cancelData($this->userId,$id);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }
        if (!$info) {
            return Show::error([], "取消失败");
        }

        return Show::success([], "取消成功");
    }

    //我发的活列表
    public function sendList(){
        $process_status = input("process_status", 0, "intval");
        $page = input("page", 0, "intval");
        $limit = input("limit", 0, "intval");

        $workBis = new \app\common\business\Work();

        //筛选条件
        $where = [
            ['user_id','=',$this->userId]
        ];
        //有效process_status
        $workBis->checkProcessStatus($process_status) && $where[] = ['process_status','=',$process_status];
        $list = $workBis->getListByWhere($where,$page,$limit,'id,department_id,name,longitude,latitude,address,start_time,price,valuation,show_time,process_status,end_time,user_id,duration,is_self');
        $totalCount = $workBis->getCountByWhere($where,'id');
        if (!empty($list)) {
            $list = $workBis->resetTextList($list);//reset
        }

        $result = [
            'count'=>$totalCount,
            'list'=>$list
        ];
        return Show::success($result, "OK");
    }

    //获取我发的零活详情
    public function sendDetail()
    {
        $id = input("id", 0, "intval");

        $workBis = new \app\common\business\Work();
        //筛选条件
        $where = [
            ['id','=',$id],
            ['user_id','=',$this->userId]
        ];
        $info = $workBis->getNormalByWhere($where);
        if (empty($info)) return Show::error([],'数据不存在');
        $info = $workBis->resetTextList([$info],$this->userId);
        $info = $workBis->resetInfo($info);
        return Show::success($info[0], "OK");
    }

    //收藏
    public function collect(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $work_id = input("work_id", 0, "intval");
        $type = input("type", 0, "intval");

        $data = [
            'work_id' => $work_id,
            'user_id' => $this->userId
        ];
        $bisObj = new WorkCollect();
        try {
            $res = $bisObj->handleData($data,$type);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }

        if($type == 1){
            $msg = '收藏';
        }else{
            $msg = '取消';
        }

        if (!$res) {
            return Show::error([], $msg."失败");
        }
        return Show::success([], $msg."成功");
    }

    //我收藏的活列表
    public function collectList(){
        $page = input("page", 0, "intval");
        $limit = input("limit", 0, "intval");

        $workCollectBis = new WorkCollect();

        //筛选条件
        $where = [
            ['user_id','=',$this->userId],
        ];
        $list = $workCollectBis->getListByWhere($where,$page,$limit);
        $totalCount = $workCollectBis->getCountByWhere($where,'id');
        if (!empty($list)) {
            $list = $workCollectBis->resetTextList($list);//reset
        }

        $result = [
            'count'=>$totalCount,
            'list'=>$list
        ];
        return Show::success($result, "OK");
    }

    //我接的活列表
    public function robList(){
        $page = input("page", 0, "intval");
        $limit = input("limit", 0, "intval");
        $type=input("type",0,"intval");
        $longitude = $this->request->param("longitude", "", "trim");
        $latitude = $this->request->param("latitude", "", "trim");

        $workRob = new WorkRob();

        //筛选条件
        $where = [
            ['user_id','=',$this->userId],
        ];
        $spaceData = [
            'longitude'=>$longitude,
            'latitude'=>$latitude
        ];
        $data = $workRob->getJoinPageList($where,$page,$limit,$type,$spaceData);
        return Show::success($data, "OK");
    }

    //抢活
    public function rob(){
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $work_id = input("work_id", 0, "intval");

        $data = [
            'work_id' => $work_id,
            'user_id' => $this->userId
        ];
        $bisObj = new \app\common\business\WorkRob();
        try {
            $res = $bisObj->handleData($data);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage());
        }

        if (!$res) {
            return Show::error([], "操作失败");
        }
        return Show::success([], "操作成功");
    }
}
